package tema1;

import java.io.*;

public class DataSetReader {
	enum StageType {
		INIT, ATTRIBUTES, INSTANCES
	};
	
	static DataSet read(String fileName) {
		AttributeSet attributeSet = new AttributeSet();
		DataSet dataSet = null;
		BufferedReader input = null;
		StageType stage = StageType.INIT;

		try {
			input = new BufferedReader(new FileReader(new File(fileName)));

			String line = null;
			String[] words;
			while ((line = input.readLine()) != null) {
				switch (stage) {
				case INIT:
					// reading the ATTRIBUTE tag
					if (!line.equals("ATTRIBUTES"))
						throw new Exception("Expecting ATTRIBUTES tag");
					else {
						stage = StageType.ATTRIBUTES;
					}
					break;
				case ATTRIBUTES:
					// reading attributes
					words = line.split(" ");
					if (words[0].equals("SYMBOLIC")) {
						attributeSet.add(new SymbolicAttribute(words[1]));
					} else if (words[0].equals("NUMERIC")) {
						attributeSet.add(new NumericAttribute(words[1]));
					} else if (words[0].equals("INSTANCES")) {
						stage = StageType.INSTANCES;
						dataSet = new DataSet(attributeSet);
					} else if (words[0].equals("")) {
						continue;
					} else {
						throw new Exception("Found incorrect line while reading attributes");
					}
					break;
				case INSTANCES:
					// reading instances
					dataSet.add(new Instance(attributeSet, line.split(",")));
					
					break;
				}
			}
		} catch (IOException ioe) {
			ioe.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return dataSet;
	}
}
